package com.huarui.saleOrder.action;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;
import com.huarui.saleOrder.service.SaleOrderService;
import com.huarui.util.FuncUtil;

public class SaleOrderAddAction extends BaseAction{

	public static final String MESS = "mess";

	private static final long serialVersionUID = -53082519840876146L;

	private SaleOrderService saleOrderService;

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}
	
	private String saleOrder;
	
	private String saleDetails;
	
	private Map<String, Object> mesMap;
	
	public Map<String, Object> getMesMap() {
		return mesMap;
	}


	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}


	public String getSaleOrder() {
		return saleOrder;
	}


	public void setSaleOrder(String saleOrder) {
		this.saleOrder = saleOrder;
	}


	public String getSaleDetails() {
		return saleDetails;
	}


	public void setSaleDetails(String saleDetails) {
		this.saleDetails = saleDetails;
	}


	@SuppressWarnings("unchecked")
	private String insertSaleOrderAndDetails() {
		JSONObject jo_order = JSONObject.fromObject(saleOrder);
		FuncUtil.removeNullValue(jo_order);
		PurOrSaleOrder obj_order = (PurOrSaleOrder) JSONObject.toBean(jo_order, PurOrSaleOrder.class);
		
		JSONArray ja_details = JSONArray.fromObject(saleDetails);
		FuncUtil.removeNullValue(ja_details);
		List<PurOrSaleOrderDetail> ds = (List<PurOrSaleOrderDetail>) JSONArray.toCollection(ja_details, PurOrSaleOrderDetail.class);
		
		mesMap = this.saleOrderService.insertSaleOrderAndDetails(obj_order, ds);
		return MESS;
	}
	
	public String saveSaleOrderAndDetails() {
		return this.insertSaleOrderAndDetails();
	}
	
	public String submitSaleOrderAndDetail() {
		return this.insertSaleOrderAndDetails();
	}
}
